package cn.gov.pbc.tims.controller;
import java.util.Date;
import java.util.Properties;

import javax.mail.Authenticator;
import javax.mail.Folder;
import javax.mail.Message;
import javax.mail.Multipart;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Store;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeUtility;
import javax.mail.search.SearchTerm;
import javax.mail.search.SubjectTerm;
public class SearchMailTest {
 
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		try {  
            Properties props=new Properties();    
            props.setProperty("mail.transport.protocol", "imap");    
            props.setProperty("mail.host", "imap.sina.cn");  
            Session session=Session.getDefaultInstance(props,   
                    new Authenticator()    
            {    
  
                protected PasswordAuthentication getPasswordAuthentication()    
                {    
                    return new PasswordAuthentication("18663766106@sina.cn","dem0gruland");    
                }    
            }  );  
  
            //连接服务器，并打开文件夹  
             Store store=session.getStore("imap");    
                store.connect("imap.sina.cn", "18663766106@sina.cn", "dem0gruland");  
            Folder folder=store.getFolder("INBOX");  
            if(folder==null){  
                System.out.println("Folder not found!");  
                System.exit(1);  
            }  
            folder.open(Folder.READ_WRITE);  
            //从服务器获取消息  
            SearchTerm search= new  SubjectTerm("最新职位信息");
            Message[] ms=folder.getMessages();
            System.out.println("搜索到"+ms.length+"封符合条件的邮件,正在打印.....");
            for(int i=0;i<ms.length;i++){  
                System.out.println("--------------Message"+(i+1)+"---------------");  
                String from =InternetAddress.toString(ms[i].getFrom());  
                if(from!=null){  
                	from=MimeUtility.decodeText(from);
                    System.out.println("消息来自："+from);  
                }  
                String to=InternetAddress.toString(ms[i].getRecipients(Message.RecipientType.TO));  
                if(to!=null){  
                	to=MimeUtility.decodeText(to);
                    System.out.println("消息去往："+to);  
                }  
                String replyTo=InternetAddress.toString(ms[i].getReplyTo());  
                if(replyTo!=null){ 
                	replyTo=MimeUtility.decodeText(replyTo);
                    System.out.println("消息回复给："+replyTo);  
                }  
                String cc=InternetAddress.toString(ms[i].getRecipients(Message.RecipientType.CC));  
                if(cc!=null){  
                    System.out.println("消息抄送："+cc);  
                }  
                Date sent=ms[i].getSentDate();  
                if(sent!=null){  
                    System.out.println("消息发送时间：："+sent);  
                }  
                String subject=ms[i].getSubject();  
                if(subject!=null){
                	subject=MimeUtility.decodeText(subject);
                    System.out.println("消息主题："+subject);  
                }  
                Date received=ms[i].getReceivedDate();  
                if(received!=null){  
                    System.out.println("消息接收时间："+received);  
                }  
                System.out.println("消息内容：");
                if(ms[i].isMimeType("multipart/*")){
                	Multipart mp=(Multipart) ms[i].getContent();
                	int bodyNum=mp.getCount();
                	for(int k=0;k<bodyNum;k++){
                		if(mp.getBodyPart(k).isMimeType("text/html")){
                			String content=(String) mp.getBodyPart(k).getContent();
                			System.out.println(content);
                		}
                	}
                }else{
                	System.out.println("不支持的邮件类型！");
                }
  
            }  
            folder.close(false);  
            store.close();  
        } catch (Exception e) {  
            e.printStackTrace();  
            System.exit(1);
        } 
	}
	}
 
